package com.yc.bigdata.flink.demo.schema;

import com.alibaba.fastjson.JSON;
import com.yc.bigdata.flink.demo.vo.UserAction;

import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.api.common.serialization.SerializationSchema;
import org.apache.flink.api.common.typeinfo.TypeInformation;

import java.io.*;
import java.nio.charset.Charset;

/**
 * <p></p>
 *
 * @author: YuanChilde
 * @date: 2020-02-15 16:49
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020-02-15 16:49    YuanChilde     1.0        新增
 */
public class UserActionSchema implements DeserializationSchema<UserAction>, SerializationSchema<UserAction> {
    @Override
    public UserAction deserialize(byte[] message) throws IOException {
        return JSON.parseObject(new String(message),UserAction.class);
    }

    @Override
    public boolean isEndOfStream(UserAction nextElement) {
        return false;
    }

    @Override
    public byte[] serialize(UserAction element) {
        return JSON.toJSONString(element).getBytes(Charset.forName("UTF-8"));
    }

    @Override
    public TypeInformation<UserAction> getProducedType() {
        return TypeInformation.of(UserAction.class);
    }
}
